package cheeese.and.crackers;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * New User Activity Class
 * @author Cheese and Crackers
 *
 */
public class NewUserActivity extends Activity {

	private UserList users;
	private Button okButton;
	private Button cancelButton;
	
	/** 
	 * Called when the activity is first created. 
     * 
     * @param savedInstanceSate Saved state of activity when last suspended
     */
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        setContentView(R.layout.newuserscreen);
        
        users = new UserList();
        
        try {
			users.load(getApplicationContext());
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), (CharSequence)("Fatal Error With User Storage - Shutting Down"), Toast.LENGTH_SHORT).show();
    		try {
    			Thread.sleep(2000);
    		} 
    		catch(InterruptedException e2) {} 
    		android.os.Process.killProcess(android.os.Process.myPid());
		}
        
        okButton = (Button)findViewById(R.id.buttonOK);
        cancelButton = (Button)findViewById(R.id.buttonCancel);
        
        NewTaskClickListener myListener = new NewTaskClickListener();
       
        okButton.setOnClickListener(myListener);
        cancelButton.setOnClickListener(myListener);
	}
	
	/**
	 * Method to call when activity is paused
	 */
	public void onPause(){
		super.onPause();
		try{
    		users.save(getApplicationContext());
    	}
    	catch(Exception e){
    		Toast.makeText(getApplicationContext(), (CharSequence)("Fatal Error With User Storage - Shutting Down"), Toast.LENGTH_SHORT).show();
    		try {
    			Thread.sleep(2000);
    		} 
    		catch(InterruptedException e2) {} 
    		android.os.Process.killProcess(android.os.Process.myPid());
    	}
	}
	
	/**
     * Listener for button
     */
	public class NewTaskClickListener implements OnClickListener {
    	@Override
    	/**
    	 * Checks which button was pressed and takes appropriate action, login or create user
    	 * @param v The view which was clicked
    	 */
		public void onClick(View v) {
    			if(v==okButton){
    				String name = ((EditText)findViewById(R.id.name)).getText().toString();
    				String email = ((EditText)findViewById(R.id.email)).getText().toString();
    				String loginId = ((EditText)findViewById(R.id.loginId)).getText().toString();
    				String password = ((EditText)findViewById(R.id.password)).getText().toString();
    				User newUser = new User(name, email, loginId, password);
    				try{
    					users.addUser(newUser);
    					Toast.makeText(getApplicationContext(), (CharSequence)("User Add Successful"), Toast.LENGTH_SHORT).show();
    					finish();
    				}
    				catch(Exception e){
    					Toast.makeText(getApplicationContext(), (CharSequence)(e.getMessage()), Toast.LENGTH_SHORT).show();
    				}
    			}	
    			else if(v==cancelButton){
    				finish();
    			}
		}
	}
}